<template>
  <div>
    <el-card>
      <template #header>
        <el-form :inline="true" :model="condition">
          <el-form-item>
            <el-input v-model="condition.userName" placeholder="请输入用户账号！" clearable ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="doDataList()">查询</el-button>
          </el-form-item>
          <div style="float: right">
            <el-button type="primary" @click="addUserHandle" >新增用户<el-icon style="margin-left: 5px"><Plus /></el-icon></el-button>
          </div>
        </el-form>
      </template>
      <el-table
        ref="enterpriseRef"
        :data="tableData"
        style="width: 100%; max-height: 61vh; height: 61vh"
        table-layout="fixed"
        v-loading="loading"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
      >
        <el-table-column fixed min-width="70" prop="userName" label="用户账号" />
        <el-table-column prop="nickName" label="用户名称" />
        <el-table-column prop="roleId" label="用户角色">
          <template #default="scope">
            <el-tag type v-if="scope.row.roleId == 1">普通用户</el-tag>
            <el-tag type v-if="scope.row.roleId == 2">管理员</el-tag>
            <el-tag type v-if="scope.row.roleId == 3">超级管理员</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="editUserHandle(scope.row)"
              >编辑</el-button
            >
            <el-popover
              placement="top-start"
              title="删除操作"
              :width="200"
              trigger="click"
              :visible="visibleSS[scope.$index]"
            >
              <p>确定要进行删除吗?</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="small"
                  text
                  @click="visibleSS[scope.$index] = false"
                  >取消</el-button
                >
                <el-button
                  size="small"
                  type="primary"
                  @click="del(scope.$index, scope.row)"
                  >确定</el-button
                >
              </div>
              <template #reference>
                <el-button
                  link
                  type="danger"
                  ref="buttonRefs"
                  @click="visibleSS[scope.$index] = true"
                  >删除</el-button
                >
              </template>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10,20,30,40,50,100,500]"
        background
        v-model:page-size="pagination.size"
        v-model:current-page="pagination.page"
        :total="pagination.total"
        @size-change="sizeChange"
        @current-change="pageChange"
        class="pagination"
      />
      <!-- 新增用户信息界面 -->
      <add-user ref="addUserRef" @refreshDataList="doDataList"></add-user>
      <!-- 修改用户信息界面 -->
      <edit-user ref="editUserRef" @refreshDataList="doDataList"></edit-user>
    </el-card>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { getUserList,delUser } from "@/api/admin/index";
import { ElMessage } from "element-plus";
import addUser from "./dialog/add-user.vue";
import editUser from "./dialog/edit-user.vue";
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0,
});
const tableData = ref([]);
//查询条件
const condition = reactive({userName:''});
const loading = ref(false);
const visibleSS = ref([]);
//获取分页用户信息列表
const doDataList = () => {
  loading.value = true;
  let datas = {
    pageNum: pagination.page,
    pageSize: pagination.size,
    condition: condition.userName,
  };
  getUserList(datas)
    .then((res) => {
      tableData.value = res.data.records;
      loading.value = false;
      pagination.size = res.data.pageSize;
      pagination.page = res.data.currentPage;
      pagination.total = res.data.total;
    })
    .catch((msg) => {});
};
/**
 * 每页显示条数改变时
 * 初始页面为1
 */
const sizeChange = (value) => {
  pagination.size = value;
  pagination.page = 1;
  doDataList();
};
/**
 * 页总数改变
 */
const pageChange = (value) => {
  pagination.page = value;
  doDataList();
};

const del = (index,rowData) =>{
  rowData
  delUser(rowData).then((res)=>{
    if(res.status==200){
      ElMessage.success(res.msg);
      doDataList();
    }else{
      ElMessage.error(res.msg);
    }
  }).catch((msg)=>{
    ElMessage.error(msg);
  })
}
doDataList();
//新增组件绑定
const addUserRef = ref()
const addUserHandle = () => {
	addUserRef.value.init()
}
//修改组件绑定
const editUserRef=ref()
const editUserHandle = (row) => {
  editUserRef.value.init(row)
  console.log("一个用户："+row)
}
</script>
<style lang="less">
.title {
  min-width: 5em;
}
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
.el-card__header {
  max-height: 10vh;
}
.pagination {
  max-height: 3vh;
}
</style>

